<template>
  <div>
    <el-dialog :title="info.title" :visible.sync="info.show ">
      <el-form :model="form">
        <el-form-item label="角色名称" :label-width="formLabelWidth">
          <el-input v-model="form.rolename"></el-input>
        </el-form-item>

        <el-form-item label="角色权限" :label-width="formLabelWidth">
          <el-tree :data="data"
           show-checkbox
            node-key="id"
             ref="tree" 
             :default-checked-keys="defaultKeys"
             :props="defaultProps">
             </el-tree>
        </el-form-item>

        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="blue"
            inactive-color="grey"
            :active-value="1"
            :inactive-value="2"
            default-checked-keys="defaultKeys"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm" v-if="info.isAdd">添加</el-button>
        <el-button type="primary" @click="update" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import { successAlert } from "../../../utils/alert";
import { addRole, oneRole, updateRole } from "../../../utils/request";
export default {
  props: ["info"],
  computed: {
    ...mapGetters({
      "data": "menu/menu",
    }),
  },
  data() {
    return {
      form: {
        rolename: "",
        menus: "",
        status: 1,
      },

      defaultProps: {
        children: "children",
        label: "title",
      },
      defaultKeys: [], //默认勾选的节点的 key 的数组

      formLabelWidth: "120px",
    };
  },
  methods: {
    cancel() {
      this.info.show = false;
      this.form = {
        rolename: "",
        menus: "",
        status: 1,
      };
      this.defaultKeys = this.$refs.tree.setCheckedKeys([]); //通过 keys 设置目前勾选的节点，使用此方法必须设置 node-key 属性
    },
    confirm() {
      this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      // console.log(this.$refs.tree.getCheckedKeys())   //若节点可被选择（即 show-checkbox 为 true），则返回目前被选中的节点的 key 所组成的数组
      //   console.log(this.form)
      addRole(this.form).then((res) => {
        successAlert(res.data.msg);
        this.cancel();
        this.requestRole();
      });
    },
     getDetail(id){
       oneRole({id}).then(res=>{
        this.form = res.data.list;
        this.form.id = id;
        this.defaultKeys = JSON.parse(this.form.menus)
        // console.log( JSON.parse(this.form.menus))
       })
     },
    update() {
      updateRole(this.form).then(res=>{
        successAlert(res.data.msg)
        this.cancel()
        this.requestRole()
      })

      // console.log(this.form) 
    },
    ...mapActions({
      "requestMenu": "menu/menuActions",
      "requestRole": "role/roleActions",
    }),
  },
  mounted() {
    this.requestMenu();
  },
};
</script>

<style scoped>
 .el-input{
     width: 220px;
   }
</style>